package com.siyu.tickets.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siyu.common.page.Pagination;
import com.siyu.common.page.SimplePage;
import com.siyu.common.web.CookieUtils;
import com.siyu.tickets.entity.Element;
import com.siyu.tickets.entity.Product;
import com.siyu.tickets.manager.ElementMng;
import com.siyu.tickets.manager.ProductMng;
import com.siyu.tickets.web.SysUtils;

/**
 * 发布产品相关
 * 
 * @since 2013-7-15
 * @version 0.1
 * @author cuigx
 */
@Controller
public class ProductAct {

	private static final Logger log = LoggerFactory.getLogger(ProductAct.class);

	@RequestMapping("/product/list")
	public String list(String name, Date beginTime, Date endTime, Integer typeId, Integer groupId, Boolean enable,
			Integer pageNo, HttpServletRequest request, ModelMap model) {
		Integer supplierId = SysUtils.getUser(request).getSupplier().getId();
		Pagination pagination = manager.getPage(name, beginTime, endTime, typeId, groupId, null, supplierId, enable,
				SimplePage.cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("name", name);
		model.addAttribute("enable", enable);
		model.addAttribute("beginTime", beginTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		return "product/list";
	}

	@RequestMapping("/product/add")
	public String add(HttpServletRequest request, ModelMap model) {

		List<Element> cityList = elementManager.findListByType(Element.ELE_TYPE_CITY);
		model.addAttribute("cityList", cityList);
		List<Element> typeList = elementManager.findListByType(Element.ELE_TYPE_TYPE);
		model.addAttribute("typeList", typeList);
		List<Element> groupList = elementManager.findListByType(Element.ELE_TYPE_GROUP);
		model.addAttribute("groupList", groupList);
		List<Element> deliveryList = elementManager.findListByType(Element.ELE_TYPE_DELIVERY);
		model.addAttribute("deliveryList", deliveryList);

		return "product/add";
	}

	@RequestMapping("/product/edit")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		Product product = manager.findById(id);
		model.addAttribute("product", product);

		List<Element> cityList = elementManager.findListByType(Element.ELE_TYPE_CITY);
		model.addAttribute("cityList", cityList);
		List<Element> typeList = elementManager.findListByType(Element.ELE_TYPE_TYPE);
		model.addAttribute("typeList", typeList);
		List<Element> groupList = elementManager.findListByType(Element.ELE_TYPE_GROUP);
		model.addAttribute("groupList", groupList);
		List<Element> deliveryList = elementManager.findListByType(Element.ELE_TYPE_DELIVERY);
		model.addAttribute("deliveryList", deliveryList);

		return "product/edit";
	}

	@RequestMapping("/product/save")
	public String save(Product bean, HttpServletRequest request, ModelMap model) {
		bean.setSupplier(SysUtils.getUser(request).getSupplier());
		bean = manager.newProduct(bean);
		return "redirect:list";
	}

	@RequestMapping("/product/update")
	public String update(Product bean, HttpServletRequest request, ModelMap model) {
		manager.updateProduct(bean);
		return "redirect:list";
	}

	@RequestMapping("/product/delete")
	public String delete(Integer[] ids, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Product[] beans = manager.deleteByIds(ids);
		for (Product bean : beans) {
			log.info("delete Product id={}", bean.getId());
		}
		return "redirect:list";
	}

	@RequestMapping("/product/enable")
	public String enable(Integer[] ids, Boolean enable, Integer pageNo, HttpServletRequest request, ModelMap model) {
		manager.enableByIds(ids, enable);
		return "redirect:list";
	}

	@Autowired
	private ProductMng manager;

	@Autowired
	private ElementMng elementManager;
}
